package cn.qayy.dto;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.servlet.ServletContext;

import cn.qayy.constants.CollectionConstant;
import cn.qayy.constants.Constant;
import cn.qayy.service.Uploadable;
import cn.qayy.utils.DateUtils;
import cn.qayy.utils.ServletContextUtils;
import cn.qayy.utils.StringUtil;

@SuppressWarnings("all")
/***
 * 标准测算价 库
 * @author jobs1127
 *
 */
public class StandardDTO implements Uploadable, Serializable {
	/** 省份 **/
	private String province = "";
	/** 品种 **/
	private String product = "";
	/** 包装 **/
	private String pack = "";
	/** 模式 **/
	private String mode = "";
	/** 条线 **/
	private String qudao = "";
	/** 渠道 **/
	private String ztype = "";
	/** 标准测算价 **/
	private String bzcsj = "";
	/** 开始时间**/
	private String stime="";
	/** 结束时间**/
	private String etime="";
	/** 修订标识 **/
	private String isUpdate = "E";
	/** id **/
	private String id = "";

	public StandardDTO() {
	}

	

	@Override
	public Uploadable filter(Map<String, List<Uploadable>> map, Set<Uploadable> set, int num) {
		/** 容纳错误消息的list */
		List<Uploadable> mlist = new ArrayList<Uploadable>(Constant.SMALL_CAPACITY);
		/** 容纳正确的，最终用于保存到数据库的数据 **/
		List<Uploadable> list = new ArrayList<Uploadable>(Constant.SMALL_CAPACITY);

		/***
		 * 过滤掉所有的空格
		 */
		String province = StringUtil.trimAll(this.getProvince());
		String product = StringUtil.trimAll(this.getProduct());
		String pack = StringUtil.trimAll(this.getPack());
		String mode = StringUtil.trimAll(this.getMode());
		String qudao = StringUtil.trimAll(this.getZtype());
		String tx = StringUtil.trimAll(this.getQudao());
		String bjcsj = StringUtil.trimAll(this.getBzcsj());
		String isUpdate = StringUtil.trimAll(this.getIsUpdate());
		String id = StringUtil.trimAll(this.getId());
		String stime = StringUtil.trimAll(this.getStime());
		String etime = StringUtil.trimAll(this.getEtime());
		
		/**
		 * 判断是否导入的模板是正确的模板
		 */
		if (num == 1) {
			if (!CollectionConstant.bck_mb.get(0).contains(province) 
					|| !CollectionConstant.bck_mb.get(1).contains(product)
					|| !CollectionConstant.bck_mb.get(2).contains(pack) 
					|| !CollectionConstant.bck_mb.get(3).contains(mode)
					|| !CollectionConstant.bck_mb.get(4).contains(tx) 
					|| !CollectionConstant.bck_mb.get(5).contains(qudao)
					|| !CollectionConstant.bck_mb.get(6).contains(bjcsj)
					|| !CollectionConstant.bck_mb.get(7).contains(stime)
					|| !CollectionConstant.bck_mb.get(8).contains(etime)
					|| !CollectionConstant.bck_mb.get(9).contains(isUpdate)
					|| !CollectionConstant.bck_mb.get(10).contains(id)) {
				MsgInfo msg = new MsgInfo(Constant.ERROR_MB, Constant.ERROR_MB, Constant.ERROR_MB,
						Constant.ERROR_NUM + num);
				mlist.add(msg);
				map.put("stop", mlist);
				return null;
			}
		} else {
			/***
			 * 获取项目上下文的共享数据
			 */
			ServletContext servletContext = ServletContextUtils.getServletContext();
			/**省份*/
			Set<String> provinces = (HashSet<String>) servletContext.getAttribute("provinces");
			/**标准测算价 重复项判断key*/
			String pbKeys = ((HashSet<String>) servletContext.getAttribute("pbKeys")).toString();

			/***
			 * 数据合格性校验
			 */
			if (!CollectionConstant.cuFlags.contains(isUpdate)) {
				MsgInfo msg = new MsgInfo(isUpdate, "修订标识", CollectionConstant.cuFlags.toString(),
						Constant.ERROR_NUM + num);
				mlist.add(msg);
			}
			if("E".equals(isUpdate)) {
				if(StringUtil.isBlank(id)) {
					MsgInfo msg = new MsgInfo(isUpdate,"修订标识有误或id有误","修订标识为E或空白时，id不能为空。id为空白时，修订标识只能是N",Constant.ERROR_NUM+num);
					mlist.add(msg);
				}
			}
			if (!provinces.contains(province)) {
				MsgInfo msg = new MsgInfo(province, "省份", Constant.BASIC_DATA, Constant.ERROR_NUM + num);
				mlist.add(msg);
			}
			/**
			 * 判断品种
			 */
			if (!CollectionConstant.products.contains(product)) {
				MsgInfo msg = new MsgInfo(product, "品种", CollectionConstant.products.toString(),
						Constant.ERROR_NUM + num);
				mlist.add(msg);
			}
			/**
			 * 判断包装
			 */
			if (!CollectionConstant.packs.contains(pack)) {
				MsgInfo msg = new MsgInfo(pack, "包装", CollectionConstant.packs.toString(), Constant.ERROR_NUM + num);
				mlist.add(msg);
			}
			/**
			 * 判断模式
			 */
			if (!CollectionConstant.modes.contains(mode)) {
				MsgInfo msg = new MsgInfo(mode, "模式", CollectionConstant.modes.toString(), Constant.ERROR_NUM + num);
				mlist.add(msg);
			}
			/**
			 * 判断条线
			 */
			if (!CollectionConstant.txs.contains(tx)) {
				MsgInfo msg = new MsgInfo(tx, "条线", CollectionConstant.txs.toString(), Constant.ERROR_NUM + num);
				mlist.add(msg);
			}
			/***
			 * 判断渠道
			 */
			if (!CollectionConstant.qudaos.contains(qudao)) {
				MsgInfo msg = new MsgInfo(qudao, "渠道类型", CollectionConstant.qudaos.toString(),
						Constant.ERROR_NUM + num);
				mlist.add(msg);
			}
			/**
			 * 标准测算价
			 */
			if (StringUtil.parseFloat(bjcsj) == null) {
				MsgInfo msg = new MsgInfo(bjcsj, "标准测算价", Constant.NUMBER, Constant.ERROR_NUM + num);
				mlist.add(msg);
			}

			/**
			 * 判断日期
			 */
			if(DateUtils.parseDate(stime) == null) {
				MsgInfo msg = new MsgInfo(stime, "开始时间", Constant.DATE_FORMAT, Constant.ERROR_NUM + num);
				mlist.add(msg);
			}
			if(DateUtils.parseDate(etime) == null) {
				MsgInfo msg = new MsgInfo(etime, "结束时间", Constant.DATE_FORMAT, Constant.ERROR_NUM + num);
				mlist.add(msg);
			}
			
			/***
			 * 判断是否在数据库中存在
			 */
			if (isUpdate.equals("N")) {
				String key = province+"-"+product+"-"+pack+"-"+mode+"-"+tx+"-"+qudao+"-"+stime+"-"+etime;
				if (pbKeys.contains(key)) {
					MsgInfo msg = new MsgInfo(key, Constant.REPEATABLE, "对应了多个标准测算价，请检查", Constant.ERROR_NUM + num);
					mlist.add(msg);
				}
			} else if(isUpdate.equals("E")) {
				String key = province+"-"+product+"-"+pack+"-"+mode+"-"+tx+"-"+qudao+"-"+stime+"-"+etime;
				if (pbKeys.contains(key)) {
					key = key+"-"+id;
					if(!pbKeys.contains(key)) {
						MsgInfo msg = new MsgInfo(key, Constant.REPEATABLE, "对应了多个标准测算价，请检查", Constant.ERROR_NUM + num);
						mlist.add(msg);
					}
				}
			}

			/***
			 * 返回处理后的结果数据
			 */
			if (!mlist.isEmpty()) {
				map.put("fail" + num, mlist);
			} 
			/*** 把验证通过的合格数据返回 **/
			this.setAll(province, product, pack, mode, tx, qudao, bjcsj, isUpdate, id);
			//不是空行才添加
			if(StringUtil.isNotBlank(product)&&StringUtil.isNotBlank(pack)&&StringUtil.isNotBlank(bjcsj)) {
				set.add(this);
			}
			return this;
		}
		return null;
	}

	/***
	 * String key = province+"-"+product+"-"+pack+"-"+mode+"-"+ztype+"-"+qudao+"-"+stime+"-"+etime;;
	 * 唯一标识该对象
	 */

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((etime == null) ? 0 : etime.hashCode());
		result = prime * result + ((mode == null) ? 0 : mode.hashCode());
		result = prime * result + ((pack == null) ? 0 : pack.hashCode());
		result = prime * result + ((product == null) ? 0 : product.hashCode());
		result = prime * result + ((province == null) ? 0 : province.hashCode());
		result = prime * result + ((qudao == null) ? 0 : qudao.hashCode());
		result = prime * result + ((stime == null) ? 0 : stime.hashCode());
		result = prime * result + ((ztype == null) ? 0 : ztype.hashCode());
		return result;
	}
	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		StandardDTO other = (StandardDTO) obj;
		if (etime == null) {
			if (other.etime != null)
				return false;
		} else if (!etime.equals(other.etime))
			return false;
		if (mode == null) {
			if (other.mode != null)
				return false;
		} else if (!mode.equals(other.mode))
			return false;
		if (pack == null) {
			if (other.pack != null)
				return false;
		} else if (!pack.equals(other.pack))
			return false;
		if (product == null) {
			if (other.product != null)
				return false;
		} else if (!product.equals(other.product))
			return false;
		if (province == null) {
			if (other.province != null)
				return false;
		} else if (!province.equals(other.province))
			return false;
		if (qudao == null) {
			if (other.qudao != null)
				return false;
		} else if (!qudao.equals(other.qudao))
			return false;
		if (stime == null) {
			if (other.stime != null)
				return false;
		} else if (!stime.equals(other.stime))
			return false;
		if (ztype == null) {
			if (other.ztype != null)
				return false;
		} else if (!ztype.equals(other.ztype))
			return false;
		return true;
	}


	public void setAll(String province, String product, String pack, String mode, String tx, String qudao, String bjcsj,
			String isUpdate, String id) {
		this.province = province;
		this.product = product;
		this.pack = pack;
		this.mode = mode;
		this.ztype = qudao;
		this.qudao = tx;
		this.bzcsj = bjcsj;
		this.isUpdate = isUpdate;
		this.id = id;
	}

	public String getId() {
		return id;
	}

	public String getIsUpdate() {
		return isUpdate;
	}

	public String getMode() {
		return mode;
	}

	public String getPack() {
		return pack;
	}

	public String getProduct() {
		return product;
	}

	public String getProvince() {
		return province;
	}

	public String getQudao() {
		return qudao;
	}


	public void setId(String id) {
		this.id = id;
	}

	public void setIsUpdate(String isUpdate) {
		this.isUpdate = isUpdate;
	}

	public void setMode(String mode) {
		this.mode = mode;
	}

	public void setPack(String pack) {
		this.pack = pack;
	}

	public void setProduct(String product) {
		this.product = product;
	}

	public void setProvince(String province) {
		this.province = province;
	}

	public void setQudao(String qudao) {
		this.qudao = qudao;
	}

	public StandardDTO(String province, String product, String pack, String mode, String qudao, String ztype,
			String bzcsj, String isUpdate, String id) {
		super();
		this.province = province;
		this.product = product;
		this.pack = pack;
		this.mode = mode;
		this.qudao = qudao;
		this.ztype = ztype;
		this.bzcsj = bzcsj;
		this.isUpdate = isUpdate;
		this.id = id;
	}


	@Override
	public String toString() {
		return "StandardDTO [province=" + province + ", product=" + product + ", pack=" + pack + ", mode=" + mode
				+ ", qudao=" + qudao + ", ztype=" + ztype + ", bzcsj=" + bzcsj + ", isUpdate=" + isUpdate + ", id=" + id
				+ "]";
	}


	public String getBzcsj() {
		return bzcsj;
	}


	public void setBzcsj(String bzcsj) {
		this.bzcsj = bzcsj;
	}


	public String getZtype() {
		return ztype;
	}

	public void setZtype(String ztype) {
		this.ztype = ztype;
	}

	public String getStime() {
		return stime;
	}


	public void setStime(String stime) {
		this.stime = stime;
	}


	public String getEtime() {
		return etime;
	}


	public void setEtime(String etime) {
		this.etime = etime;
	}


	@Override
	public String flag() {
		return this.isUpdate;
	}

}
